#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <sys/socket.h>


#define BACKLOG  10
#define SERVPORT 8001
#define MAXDATASIZE 100 /*每次最大数据传输量 */

main(int argc, char *argv[]){
	int i;
	int addr_len =sizeof(struct sockaddr_in); 
	int sockfd;
	int pid;
	char buff_out[MAXDATASIZE],buff_in[MAXDATASIZE];
	int size_out,size_in;
	struct sockaddr_in serv_addr,client_addr;

	if ((sockfd = socket(AF_INET, SOCK_DGRAM, 0)) == -1){
		perror("socket创建出错！");
		exit(1);
	}
	serv_addr.sin_family=AF_INET;
	serv_addr.sin_port=htons(SERVPORT);
	serv_addr.sin_addr.s_addr = INADDR_ANY; /* 填入本机IP地址 */
	memset(serv_addr.sin_zero,0,sizeof(serv_addr.sin_zero));

	if (bind(sockfd, (struct sockaddr *)&serv_addr, sizeof(struct sockaddr)) == -1) {
		perror("bind error");
		exit(1);
	}
	
	printf("fuck");

	while(1){
		size_in = recvfrom(sockfd, buff_in, MAXDATASIZE, 0, (struct sockaddr*)&client_addr, &addr_len); 
		if(size_in == -1){
			perror("recv error");
			continue;
		}
		if(size_in){
				buff_in[size_in] = '\0';
                buff_out[0] = atoi(argv[1]);
				//memcpy(buff_out,buff_in,sizeof(buff_in));
				size_out = 1;
				sendto(sockfd, buff_out, MAXDATASIZE, 0, (struct sockaddr*)&client_addr, sizeof(struct sockaddr)); 
			}
			else continue;
		}
	
}
